import { Util } from '../../utils/util';
import { DateUtil } from '../../utils/dateUtil';
import { StoreEmployeeRelation } from './storeEmployee';
import { Engine } from '../engine';

export class Store {
    constructor({
        id,
        name,
        contactPerson = '',
        contactPhoneNum = '',
        addr = '',
        enabled = true,
        remark = '',
        createTime = null,
        publicStore = false,
        employees = []
    }) {
        this.id = id;
        this.name = name;
        this.contactPerson = contactPerson;
        this.contactPhoneNum = contactPhoneNum;
        this.addr = addr;
        this.enabled = enabled;
        this.remark = remark;
        this.createTime = createTime;
        this._publicStore = publicStore;
        this._storeEmployeeList = employees;
    }

    static empty({ id = '', name = '', contactPerson = '', contactPhoneNum = '', addr = '', enabled = true, remark = '', createTime = null, publicStore = false } = {}) {
        return new Store({
            id,
            name,
            contactPerson,
            contactPhoneNum,
            addr,
            enabled,
            remark,
            createTime,
            publicStore,
            employees: []
        });
    }

    static clone(store) {
        const cloned = new Store({
            id: store.id,
            name: store.name,
            contactPerson: store.contactPerson,
            contactPhoneNum: store.contactPhoneNum,
            addr: store.addr,
            enabled: store.enabled,
            remark: store.remark,
            createTime: store.createTime,
            publicStore: store._publicStore,
            employees: store._storeEmployeeList.map(item => item)
        });
        return cloned;
    }

    static fromJson(json) {
        const employees = (json.employees || []).map(item => StoreEmployeeRelation.fromJson(item));
        return new Store({
            id: Util.jsonToString(json.id),
            name: Util.jsonToString(json.storeName),
            contactPerson: Util.jsonToString(json.contactUser),
            contactPhoneNum: Util.jsonToString(json.contactPhone),
            addr: Util.jsonToString(json.storeAddress),
            remark: Util.jsonToString(json.remark),
            enabled: Util.jsonToBool(json.enabled, true),
            publicStore: Util.jsonToBool(json.defaultStore, false),
            createTime: json.createTime ? Util.jsonToDateTime(json.createTime) : null,
            employees
        });
    }

    static fromJsonForRelatedSource(json) {
        return new Store({
            id: Util.jsonToString(json.storeId),
            name: Util.jsonToString(json.storeName),
            contactPerson: Util.jsonToString(json.storeContactUser),
            contactPhoneNum: Util.jsonToString(json.storecontactPhone),
            addr: Util.jsonToString(json.storeAddress),
            remark: Util.jsonToString(json.storeremark),
            enabled: Util.jsonToBool(json.storeenabled, true),
            publicStore: Util.jsonToBool(json.defaultStore, false),
            createTime: null,
            employees: []
        });
    }

    // 拷贝传参用，与 fromJsonForRelatedSource 一一对应
    toCopyJsonForRelatedSource() {
        const ret = {
            storeId: this.id,
            storeName: this.name,
            storeContactUser: this.contactPerson,
            storecontactPhone: this.contactPhoneNum,
            storeAddress: this.addr,
            storeremark: this.remark,
            storeenabled: this.enabled ? 1 : 0,
            defaultStore: this.publicStore ? 1 : 0,
        };
        return ret;
    }

    get publicStore() {
        return this._publicStore;
    }

    get storeEmployeeList() {
        return [...this._storeEmployeeList];
    }

    /// 判断是否是商户级别授权账户
    get isMerchantSuperStore() {
        return this.id === '0';
    }

    get employeeList() {
        return this._storeEmployeeList.map(item => item.employee);
    }

    addOrUpdateEmployee(employee) {
        const exist = this._storeEmployeeList.find(e => e.id === employee.id);
        if (!exist) {
            this._storeEmployeeList.push(employee);
        }
    }

    setEmployee(employees) {
        this._storeEmployeeList = this._storeEmployeeList.filter(rel =>
            employees.some(emp => emp.id === rel.employee.id)
        );
        employees.forEach(emp => {
            const exists = this._storeEmployeeList.some(rel => rel.employee.id === emp.id);
            if (!exists) {
                this._storeEmployeeList.push(new StoreEmployeeRelation({ id: '', store: Store.empty({ id: this.id }), employee: emp }));
            }
        });
    }

    removeEmployee(idList) {
        this._storeEmployeeList = this._storeEmployeeList.filter(rel => !idList.includes(rel.employee.id));
    }

    toJson() {
        const ret = {
            orgId: Engine.profile?.org?.orgId,
            storeName: this.name,
            contactUser: this.contactPerson,
            contactPhone: this.contactPhoneNum,
            storeAddress: this.addr,
            remark: this.remark,
            enabled: this.enabled ? 1 : 0,
            employees: this._storeEmployeeList.map(e => e.toJson())
        };
        if (this.id) {
            ret.id = this.id;
        }
        if (this.createTime) {
            ret.createTime = DateUtil.getDateTimeTitle(this.createTime);
        }
        return ret;
    }

    // 用于拷贝传参 与 fromJson 一一对应
    toCopyJson() {
        const ret = {
            id: this.id,
            storeName: this.name,
            contactUser: this.contactPerson,
            contactPhone: this.contactPhoneNum,
            storeAddress: this.addr,
            remark: this.remark,
            enabled: this.enabled,
            defaultStore: this.publicStore ? 1 : 0, // Dart 为 true 表启用，JS 中 1 表启用
            employees: this._storeEmployeeList.map(e => e.toCopyJson())
        };
        if (this.createTime) {
            ret.createTime = this.createTime;
        }
        return ret;

    }


}
